package com.sinoeyes.controller.system;

import com.sinoeyes.auth.core.util.SecurityUtils;
import com.sinoeyes.common.constant.UserConstants;
import com.sinoeyes.common.core.controller.BaseController;
import com.sinoeyes.common.core.page.RestPageResult;
import com.sinoeyes.common.result.RCode;
import com.sinoeyes.common.result.Result;
import com.sinoeyes.framework.aspectj.lang.annotation.Log;
import com.sinoeyes.framework.aspectj.lang.enums.BusinessType;
import com.sinoeyes.system.filter.SysRoleFilter;
import com.sinoeyes.system.service.ISysRoleService;
import com.sinoeyes.system.vo.SysRoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色信息
 *
 */
@RestController
@RequestMapping("/system/role")
@Api(value = "角色信息 API", tags = {"角色信息"})
public class SysRoleController extends BaseController
{
    @Autowired
    private ISysRoleService roleService;

    @PreAuthorize("@ss.hasPermi('system:role:list')")
    @GetMapping("/list")
    @ApiOperation("角色分页查询")
    public Result<RestPageResult<SysRoleVo>> list(SysRoleFilter roleFilter)
    {
        RestPageResult<SysRoleVo> pageResult = roleService.selectRoleList(roleFilter);
        return Result.success(pageResult);
    }

    /**
     * 根据角色编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping(value = "/{roleId}")
    @ApiOperation("根据角色编号获取详细信息")
    public Result<SysRoleVo> getInfo(@PathVariable Long roleId)
    {
        return Result.success(roleService.selectRoleById(roleId));
    }

    /**
     * 新增角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:add')")
    @Log(title = "角色管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增角色")
    public Result<Integer> add(@RequestBody SysRoleVo role)
    {
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setCreateBy(SecurityUtils.getUsername());
        return Result.success(roleService.insertRole(role));

    }

    /**
     * 修改保存角色
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改保存角色")
    public Result<Integer> edit(@RequestBody SysRoleVo role)
    {
        roleService.checkRoleAllowed(role);
        if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleNameUnique(role)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        }
        else if (UserConstants.NOT_UNIQUE.equals(roleService.checkRoleKeyUnique(role)))
        {
            return Result.failure(RCode.PARAM_VALID_ERROR.getCode(),"修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        }
        role.setUpdateBy(SecurityUtils.getUsername());
        return Result.success(roleService.updateRole(role));
    }

    /**
     * 修改保存数据权限
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/dataScope")
    @ApiOperation("修改保存数据权限")
    public Result<Integer> dataScope(@RequestBody SysRoleVo role)
    {
        roleService.checkRoleAllowed(role);
        return Result.success(roleService.authDataScope(role));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('system:role:edit')")
    @Log(title = "角色管理", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    @ApiOperation("状态修改")
    public Result<Integer> changeStatus(@RequestBody SysRoleVo role)
    {
        roleService.checkRoleAllowed(role);
        role.setUpdateBy(SecurityUtils.getUsername());
        return Result.success(roleService.updateRoleStatus(role));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@ss.hasPermi('system:role:remove')")
    @Log(title = "角色管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{roleId}")
    @ApiOperation("删除岗位")
    public Result<Integer> remove(@PathVariable Long roleId)
    {
        roleService.checkRoleAllowed(new SysRoleVo(roleId));
        return Result.success(roleService.deleteRoleById(roleId));
    }

    /**
     * 获取角色选择框列表
     */
    @PreAuthorize("@ss.hasPermi('system:role:query')")
    @GetMapping("/optionselect")
    @ApiOperation("获取角色选择框列表")
    public Result<List<SysRoleVo>> optionselect()
    {
        return Result.success(roleService.selectRoleAll());
    }
}